<?php
include_once("functions/db_manipulate.php");

if (!init()) {
  redirect("?action=login");
} else if (!canSeeTopic($_GET['tid'])) {
  redirect("?action=index");
}

$topicResultSet = getTopic($_GET['tid']);
if ($topicResultSet == null) {
  echo "ERROR";
  redirect("?action=login");
}

$topic = mysql_fetch_assoc($topicResultSet);

if ($_GET['page'] == '') {
  $start = 1;
  $page = 1;
} else {
  $start = ($_GET['page'] - 1) * 20 + 1;
  $page = $_GET['page'];
}

$topicCommentsSet = getTopicComments($start, 20, $topic['id']);

$msg = countTopicMessages($topic['id']);
$pages = pagesFromMessages($msg);

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=<?= getMessageByKey("default.charset") ?>"/>
  <title><?= getMessageByKey("topic.title") ?></title>
  <link rel="stylesheet" type="text/css" href="css/navigation.css">
  <link rel="stylesheet" type="text/css" href="css/menu.css">
  <link rel="stylesheet" type="text/css" href="css/common.css">
  <link rel="stylesheet" type="text/css" href="css/pages-template.css">
  <link rel="stylesheet" type="text/css" href="css/comment-template.css">
  <link rel="stylesheet" type="text/css" href="css/topic.css">
  <script src="resources/jquery.js"></script>

  <script language="JavaScript">
    function reply(i) {
      $('#reply').val(i);
      $('#reply2').val(i);
      $('#textarea').focus();
      $('#textarea').attr("placeholder", "<?= getMessageByKey("user.comment.new.placeholder.quote") ?>")
    }
    function customFormSubmit() {
      $('#maximized_navigation').show();
      $('#commentform').submit();
    }

    function editComment(i) {
      $('#content' + i).fadeOut('slow');
      $('#edit' + i).fadeIn('slow');
      $('#editcontent' + i).fadeIn('slow');
    }

    function deleteCommentQuote(i) {
      $('#delquote' + i).val(1);
      $('#commentquote' + i).fadeOut('slow');
      $('#edit' + i).fadeOut('slow');
    }

    function cancelEditing(i) {
      $('#commentquote' + i).fadeIn('slow');
      $('#edit' + i).fadeOut('slow');
      $('#content' + i).fadeIn('slow');
      $('#editcontent' + i).fadeOut('slow');
    }
  </script>
</head>
<body>

<?php
include_once("menu.php");
?>
<div class="maincommon">

  <div class="topicheader">
    <?= processText($topic['header']) ?>
  </div>
  <div class="topicabout">
    <?= processText($topic['content']) ?>
  </div>
  <div class="topicinfo">
    <div class="td">
      <?= getMessageByKey('topic.author') ?> <?= $topic['author'] ?>
    </div>
    <div class="td">
    </div>
    <div class="td">
      <?= getMessageByKey("general.date") ?> <?= $topic['date'] ?>
    </div>
    <div class="td">
      <?= getMessageByKey("general.time") ?> <?= $topic['time'] ?>
    </div>
  </div>


  <?php
  if (!isSubscribedToTopic($topic['id'])) {
    ?>

    <div class="manipulationbutton" style="margin-left: 680px; margin-top: 15px;"
         onclick="location.href = '?action=topicsbscr&tid=<?= $topic['id'] ?>'">
      <?= getMessageByKey("button.user.topic.subscribe") ?>
    </div>
  <?php
  }
  ?>


  <?php

  generateTopicPages($pages, $page, $topic['id']);

  while ($commentRow = mysql_fetch_assoc($topicCommentsSet)) {
    ?>
    <a name="comment<?= $commentRow['id'] ?>"> </a>
    <!--  --><?php
    $paramArray = null;
    $paramArray['comment.id'] = $commentRow['id'];
    $paramArray['comment.date'] = $commentRow['date'];
    $paramArray['comment.time'] = $commentRow['time'];
    $paramArray['user.image'] = \controller\Config::getPropertyByKey('directory.preview.user.image') . $commentRow['img'];
    $paramArray['user.registration'] = $commentRow['registered'];
    $paramArray['user.nick'] = getTextWithAcronym($commentRow['author'], 15);
    $paramArray['user.email'] = getTextWithAcronym($commentRow['email'], 22);
    $paramArray['user.messages'] = $commentRow['msg'];


    if ($commentRow['quote'] != 0) {
      $quoteRow = mysql_fetch_assoc(getTopicComment($commentRow['quote']));
      $quoteRow['content'];
      $paramArray['quote.author'] = $quoteRow['author'];
      $paramArray['quote'] = $quoteRow['content'];
    }

    $paramArray['content'] = processText($commentRow['content']);

    $paramArray['button.like']['link'] = "";
    if (!isLiked("t", $commentRow['id'])) {
      $paramArray['button.like']['link'] = "location.href = '?action=liketopic&cid=" . $commentRow['id'] . "&tid=" . $_GET['tid'] . "&like=1'";
    }
    $paramArray['button.like']['text'] = getMessageByKey("button.user.comment.likes") . "(" . $commentRow['likes'] . ")";

    $paramArray['button.dislike']['link'] = "";
    if (!isDisliked("t", $commentRow['id'])) {
      $paramArray['button.dislike']['link'] = "location.href = '?action=liketopic&cid=" . $commentRow['id'] . "&tid=" . $_GET['tid'] . "&dislike=1'";
    }
    $paramArray['button.dislike']['text'] = getMessageByKey("button.user.comment.dislikes") . "(" .
        $commentRow['dislikes'] . ")";

    $isSpammed = isSpammed("t", $commentRow['id']);

    $paramArray['button.spam']['link'] = "location.href = '?action=spamtopic&cid=" . $commentRow['id'] . "'";
    $paramArray['button.spam']['text'] = getMessageByKey("button.user.comment.spam");

    $paramArray['button.delete']['link'] = "$('#deletediv" . $commentRow['id'] . "').slideDown('slow')";
    $paramArray['button.delete']['text'] = getMessageByKey("button.general.delete");

    $paramArray['button.edit']['link'] = "editComment(" . $commentRow['id'] . ")";
    $paramArray['button.edit']['text'] = getMessageByKey("button.general.edit");

    $paramArray['button.reply']['link'] = "reply('" . $commentRow['id'] . "')";
    $paramArray['button.reply']['text'] = getMessageByKey("button.user.comment.reply");

    $paramArray['button.profile']['link'] = "location.href='?action=profile&uid=" . $commentRow['uid'] . "'";
    $paramArray['button.profile']['text'] = getMessageByKey("button.comment.user.profile");

    $paramArray['button.message']['link'] = "location.href='?action=convch&uid=" . $commentRow['uid'] . "'";
    $paramArray['button.message']['text'] = getMessageByKey("button.comment.user.message");

    $paramArray['button.edit.comment.cancel']['link'] = "cancelEditing(" . $commentRow['id'] . ")";
    $paramArray['button.edit.comment.cancel']['text'] = getMessageByKey("button.general.cancel");

    $paramArray['button.edit.comment']['link'] = "$('#editform" . $commentRow['id'] . "').submit()";
    $paramArray['button.edit.comment']['text'] = getMessageByKey("button.general.edit");

    $paramArray['button.delete.ok']['link'] = "location.href='?action=deltopiccomment&tid=" . $_GET['tid'] . "&cid=" . $commentRow['id'] . "'";
    $paramArray['button.delete.ok']['text'] = getMessageByKey("button.general.delete");

    $paramArray['button.delete.cancel']['link'] = "$('#deletediv" . $commentRow['id'] . "').slideUp('slow')";
    $paramArray['button.delete.cancel']['text'] = getMessageByKey("button.general.cancel");

    $paramArray['edit.action'] = "edittopiccomhand";
    $paramArray['delete.text'] = getMessageByKey("user.comment.drop.ask");

    $paramArray['user.id'] = $commentRow['uid'];

    $imgResultSet = getTopicCommentImages($commentRow['id']);
    $images = "";

    $facebookImage = '';

    while ($imgRow = mysql_fetch_assoc($imgResultSet)) {
      if ($facebookImage == '') {
        $facebookImage == $imgRow['path'];
      }

      $images .= "<img onclick=\"location.href='?action=fullimg&sec=topic&ipath=" . $imgRow['path'] . "'\"
           src=\"" . \controller\Config::getPropertyByKey('directory.preview.topic') . $imgRow['path'] . "\"/>";
    }

    $isOwner = false;
    if ($_SESSION['id'] == $commentRow['uid']) {
      $isOwner = true;
    }

    $paramArray['i'] = $commentRow['id'];
    $paramArray['input.hidden'] = "<input type=\"hidden\" name=\"delquote\" id=\"delquote" . $commentRow['id'] . "\"/>
  <input type=\"hidden\" name=\"cid\" value=\"" . $commentRow['id'] . "\"";


    $paramArray['images'] = $images;

    $facebookTitle = getMessageByKey("general.author") . ":" . $commentRow['author'];
    $facebookDescription = $commentRow['content'];

    $paramArray['button.facebook']['link'] = facebookShare($facebookTitle, $facebookDescription, $facebookImage);
    $paramArray['button.facebook']['text'] = "Facebook.com";

    echo generateTopicComment($paramArray, $isOwner, $isSpammed, init());
    ?>
  <?php
  }
  ?>

  <a name="lmsg"></a>

  <a id="anchor">
    <form enctype="multipart/form-data" accept-charset="UTF-8" action="?action=topiccomhand" method="post"
          id="commentform">

      <div class="newcomment">
        <div class="info">
          <?= getMessageByKey("user.comment.new") ?>
        </div>
        <textarea name="content" id="textarea" rows="11"
                  placeholder="<?= getMessageByKey("user.comment.new.placeholder") ?>"></textarea>
        <input type="hidden" name="MAX_FILE_SIZE" value="25000000"/>

        <input type="hidden" name="tid" value="<?= $_GET['tid'] ?>">
        <input type="hidden" name="reply" id="reply">

        <div class="buttonsdiv">


          <div class="fileselect">
            <input name="images[]" multiple required type="file" accept=".jpg, .png, .jpeg, .gif" id="realfileinput"
                   onmouseover="$('#fakefileinput').css('background-color', 'rgb(73, 67, 255)')"
                   onmouseout="$('#fakefileinput').css('background-color', 'rgba(0,0,0,1)')"
                   onclick="$('#galvisdiv').slideDown()"/>

            <div class="button" id="fakefileinput">
              <?= getMessageByKey("button.general.images") ?>
            </div>
          </div>


          <div class="button" onclick="customFormSubmit()">
            <?= getMessageByKey("button.general.add") ?>
          </div>
        </div>
      </div>
    </form>
  </a>


</div>
<?php
include_once("navigation.php");
?>

</body>
</html>